package com.sca.point.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sca.point.entity.Point;
import com.sca.point.repository.PointMapper;
import com.sca.point.service.PointService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuxianfei
 * @since 2020/11/13 16:19
 */
@Service
public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements PointService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPoint(Integer userId, Integer point) {
        Point one = getOne(new LambdaQueryWrapper<Point>().eq(Point::getUserId, userId));
        if (one != null) {
            int temp = one.getPoint() + point;
            Point update = new Point();
            update.setId(one.getId());
            update.setVersion(one.getVersion());
            update.setPoint(temp);
            return this.updateById(update);
        }
        one = new Point();
        one.setUserId(userId);
        one.setPoint(point);
        return this.save(one);
    }

}
